package com.ypaco.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.xml.client.Document;
import com.ypaco.ajax.client.*;
import com.google.gwt.xml.client.*;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class login_form implements EntryPoint {
	
	final RemoteXML rq=new RemoteXML(OSServers.mainserver()+"/inc/services/login.php","POST");
	
	public void onModuleLoad() {
		
		final TextBox uname=new TextBox();
		final PasswordTextBox upass=new PasswordTextBox();
		final Label err=new Label();
		final Button btn=new Button("Войти");
		RootPanel.get("user_txt").add(uname);
		RootPanel.get("pswd_txt").add(upass);
		RootPanel.get("error_lbl").add(err);
		RootPanel.get("login_btn").add(btn);
		
		class MyHandler implements ClickHandler,KeyUpHandler{
			public void onClick(ClickEvent event) {
				if (uname.getText().isEmpty())
					Window.alert("Введите имя пользователя");
				else if(upass.getValue().isEmpty())
					Window.alert("Введите пароль");
				else
					fireRequest();
			}

			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					if (!uname.getText().isEmpty() && !upass.getText().isEmpty())
						onClick(null);
				}
			}
			
			public void fireRequest()
			{
				String data="user="+uname.getText()+"&password="+upass.getText();
				err.setText("Проверка пользователя");
				rq.request(data, new RemoteXMLRequestCallback(){

					public void onFailure(Throwable ex) {
						err.setText("Ошибка связи:"+ex.getMessage());					
					}

					public void onSuccess(Document xml) {
						try
						{
							XmlProcessor proc=new XmlProcessor(xml);
							Node root=proc.getRootNode("login_result");
							Node res=proc.getNeedAttr(root, "result");
							if (res.getNodeValue().compareTo("1")==0)
							{
								res=proc.getNeedAttr(root, "usess");
								Cookies.setCookie("tt_sess", res.getNodeValue(), null, null, "/", false);
								Cookies.setCookie("tt_sess", res.getNodeValue(), null, ".1000turov.ru", "/", false);
								Cookies.setCookie("tt_sess", res.getNodeValue(), null, ".tour-agentstvo.ru", "/", false);
//								Cookies.setCookie("tt_sess", res.getNodeValue());
								Window.Location.reload();
							}else {
								err.setText("Неверное имя пользователя или пароль");
								upass.setFocus(true);
								upass.selectAll();
							}
						}catch(Throwable ex)
						{
							err.setText("Ошибка сервера:"+ex.getMessage());
						}
						
					}
				
				});
			}
		}
		
		MyHandler hndl=new MyHandler();
		btn.addClickHandler(hndl);
		upass.addKeyUpHandler(hndl);
		uname.addKeyUpHandler(hndl);
	}
}
